package com.javen.controller;

import com.alibaba.fastjson.JSONObject;
import com.javen.model.Car;
import com.javen.model.Deal;
import com.javen.model.Equipment;
import com.javen.service.ICarService;
import com.javen.service.IDealService;
import com.javen.service.IEquipmentService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping({"/deal"})
public class DealController {
    private static Logger log = LoggerFactory.getLogger(DealController.class);
    @Resource
    private IDealService dealService;
    @Resource
    private IEquipmentService equipService;
    @Resource
    private ICarService carService;

    @RequestMapping(value = {"/showdeals"}, method = {org.springframework.web.bind.annotation.RequestMethod.GET})
    @ResponseBody
    public List<Deal> showDealsByUsername(HttpServletRequest request, Model model) {
        String username = request.getParameter("username");
        List<Deal> deals = this.dealService.showDealsByUsername(username);
        return deals;
    }


    @RequestMapping(value = {"/confirmdeal"}, method = {org.springframework.web.bind.annotation.RequestMethod.GET})
    @ResponseBody
    public JSONObject confirmDeal(HttpServletRequest request, Model model) {
        int id = Integer.parseInt(request.getParameter("id"));
        String equip_id = request.getParameter("equip_id");
        String username = request.getParameter("username");
        String licence_plate = request.getParameter("licence_plate");
        String start_time = request.getParameter("start_time");

        Date date = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd :HH:mm:ss");
        String end_time = dateFormat.format(date).toString();
        int completed = 0;

        Deal deal = new Deal();
        deal.setId(Integer.valueOf(id));
        deal.setEndTime(end_time);
        deal.setCompleted(Integer.valueOf(completed));

        int res = 0;
        try {
            res = this.dealService.updateDeal(deal);
        } catch (Exception exp) {
            log.info(exp.toString());
        }
        JSONObject json = new JSONObject();
        if (res == 1) {
            json.put("res_deal", Integer.valueOf(1));
            System.out.println("update_deal success!");
        } else {
            json.put("res_deal", Integer.valueOf(0));
            System.out.println("update_deal failed!");
            return json;
        }

        int state = 0;
        int res_equip = 0;
        Equipment equip = new Equipment();
        equip.setState(Integer.valueOf(state));
        equip.setEquipId(equip_id);
        try {
            res_equip = this.equipService.updateByEquipId(equip);
        } catch (Exception exp) {
            log.info(exp.toString());
        }
        if (res_equip == 1) {
            json.put("res_equip", Integer.valueOf(1));
            System.out.println("update_equip success!");
        } else {
            json.put("res_equip", Integer.valueOf(0));
            System.out.println("update_equip failed!");
            return json;
        }

        int status = 0;
        Car car = new Car();
        car.setLicencePlate(licence_plate);
        car.setStatus(Integer.valueOf(status));
        int res_car = 0;
        try {
            res_car = this.carService.updateStatusByLicence(car);
        } catch (Exception exp) {
            log.info(exp.toString());
        }
        if (res_car == 1) {
            json.put("res_car", Integer.valueOf(1));
            System.out.println("update_car success!");
        } else {
            json.put("res_car", Integer.valueOf(0));
            System.out.println("update_car failed!");
            return json;
        }
        return json;
    }

    @RequestMapping(value = {"/deldeal"}, method = {org.springframework.web.bind.annotation.RequestMethod.GET})
    @ResponseBody
    public JSONObject delDeal(HttpServletRequest request, Model model) {
        int id = Integer.parseInt(request.getParameter("id"));
        int res = 0;
        try {
            res = this.dealService.delDeal(Integer.valueOf(id));
        } catch (Exception exp) {
            log.info(exp.toString());
        }
        JSONObject json = new JSONObject();
        if (res == 1) {
            json.put("res", Integer.valueOf(1));
            System.out.println(" delete success");
        } else {
            json.put("res", Integer.valueOf(0));
            System.out.println("delete failed");
        }
        return json;
    }
}


